package com.summer.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import com.google.common.collect.Lists;
import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.Category;
import com.summer.mall.entity.Product;
import com.summer.mall.mapper.CategoryMapper;
import com.summer.mall.mapper.ProductMapper;
import com.summer.mall.service.CategoryService;
import com.summer.mall.service.ProductService;
import com.summer.mall.util.DateTimeUtil;
import com.summer.mall.vo.ProductDetailVo;
import com.summer.mall.vo.ProductListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryService categoryService;

    @Override
    public ResponseService<String> saveOrUpadteProduct(Product product) {
        if(product != null){

            if (StringUtils.isNotBlank(product.getSubImages())){
                String[] subImage = product.getSubImages().split(",");
                if (subImage.length > 0){
                    product.setMainImage(subImage[0]);
                }
            }

            if(product.getId() != null){
                int rowCount = productMapper.updateByPrimaryKeySelective(product);
                if (rowCount > 0){
                    return ResponseService.createBySuccessMessage("更新产品信息成功");
                }else{
                    return ResponseService.createByErrorMessage("更新产品信息失败");
                }
            }else{
                int rowCount = productMapper.insertSelective(product);
                if (rowCount > 0){
                    return ResponseService.createBySuccessMessage("添加产品信息成功");
                }else{
                    return ResponseService.createByErrorMessage("添加产品信息失败");
                }
            }

        }
        return ResponseService.createByCodeErrorMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"新增和更新产品信息参数不正确");
    }

    @Override
    public ResponseService<String> setSaleStatus(Integer productId, Integer status) {
        return null;
    }

    @Override
    public ResponseService<ProductDetailVo> manageProductDetail(Integer productId) {
        return null;
    }

    @Override
    public ResponseService<PageInfo> getProductList(Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public ResponseService<PageInfo> searchProductByProductNameAndId(Integer pageNum, Integer pageSize, Integer productId, String productName) {
        return null;
    }


    /**
     * @author complone
     * @Param 分类所属的商品Id
     * @Param 商品名称
     * @Param 每一页需要显示的分页页数
     * @Param 当前第几页
     * @Param 商品排序方式
     * @Description
     * @Date 9:59 2018/7/9
     * @return
     **/
    @Override
    public ResponseService<PageInfo> getProductByKeywordCategoryId(Integer categoryId, String keyword, Integer pageNum,
                                                                   Integer pageSize, String orderBy) {
        if (StringUtils.isBlank(keyword) & categoryId == null){
            return ResponseService.createByCodeErrorMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc()+"  关键字和Category不能都为空");
        }
        List<Integer> categoryIdList =  Lists.newArrayList();
        if (categoryId != null){
            Category category = categoryMapper.selectByPrimaryKey(categoryId);
            if (category== null && StringUtils.isBlank(keyword)){
                //没有该分类,并且还没有符合关键字,这个时候返回一个空的结果集,不报错,他不是一个错误
                PageHelper.startPage(pageNum,pageSize);
                List<ProductListVo> listvo = Lists.newArrayList();
                PageInfo pageinfo = new PageInfo(listvo);
                return ResponseService.createBySuccess(pageinfo);
            }

            categoryIdList = categoryService.selectCategoryAndChildrenById(category.getId()).getData();
        }
        if (StringUtils.isNotBlank(keyword)){
            keyword  = new StringBuilder().append("%").append(keyword).append("%").toString();
        }
        PageHelper.startPage(pageNum,pageSize);
        //排序处理
        if (StringUtils.isNotBlank(orderBy)){
            if (Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
                String[] orderByArray = orderBy.split("_");
                PageHelper.orderBy(orderByArray[0]+" " + orderByArray[1]);
            }
        }
        List<Product> productList = productMapper.selectByNameAndCategoryIds(categoryIdList.size() == 0 ? null : categoryIdList, StringUtils.isBlank(keyword) ? null : keyword);

        List<ProductListVo> productVo = Lists.newArrayList();
        for (Product item : productList){
            ProductListVo productListVo =  assembleProductListVo(item);
            productVo.add(productListVo);

        }
        PageInfo pageResult = new PageInfo(productList);
        pageResult.setList(productVo);
        return ResponseService.createBySuccess(pageResult);


    }

    private  ProductDetailVo assembleProductDetailVo(Product product){
        ProductDetailVo productDetailVo = new ProductDetailVo();
        productDetailVo.setId(product.getId());
        productDetailVo.setStatus(product.getStatus());
        productDetailVo.setCategoryId(product.getCategoryId());
        productDetailVo.setName(product.getName());
        productDetailVo.setDetail(product.getDetail());
        productDetailVo.setPrice(product.getPrice());
        productDetailVo.setStock(product.getStock());
        productDetailVo.setSubtitle(product.getSubtitle());
        productDetailVo.setMainImage(product.getMainImage());
        productDetailVo.setSubImages(product.getSubImages());


        //parentId
        Category category = categoryMapper.selectByPrimaryKey(productDetailVo.getCategoryId());
        if (category != null){
            productDetailVo.setParentCategoryId(category.getParentId());
        }

        //creatTime OR updateTime dateUtils 实现时间的格式化 字符串的毫秒值到
        productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
        productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));

        return productDetailVo;
    }

    //组装 作用 :后台的分页不是POJO的每一项都要展示
    private ProductListVo assembleProductListVo(Product product){
        ProductListVo productListVo  = new ProductListVo();
        productListVo.setId(product.getId());
        productListVo.setStatus(product.getStatus());
        productListVo.setMainImage(product.getMainImage());
        productListVo.setPrice(product.getPrice());
        productListVo.setCategoryId(product.getCategoryId());
        productListVo.setName(product.getName());
        productListVo.setSubtitle(product.getSubtitle());

        return productListVo;
    }
    @Override
    public ResponseService<ProductDetailVo> productDetail(Integer productId) {
        if (productId != null){
            Product product = productMapper.selectByPrimaryKey(productId);
            if(product == null){
                return ResponseService.createByErrorMessage("产品不存在或以下架");
            }
            if(product.getStatus() != Const.ProductEnumStatus.ON_SALE.getCode()){
                return ResponseService.createByErrorMessage("产品已下架");
            }

            ProductDetailVo productDetailVo = assembleProductDetailVo(product);
            return ResponseService.createBySuccess(productDetailVo);
        }
        return ResponseService.createByCodeErrorMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    }

    @Override
    public ResponseService<Product> testFindProduct(Integer productId){
        Product product = productMapper.selectByPrimaryKey(productId);

        return ResponseService.createBySuccess("查询商品成功",product);
    }
}
